﻿using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;
using VIW = CPPEI.ETL.Domain.Views;

namespace CPPEI.ETL.WebApi.Task
{
    /// <summary>
    /// 重新执行批次，重新抽取数据
    /// </summary>
    [CPPEI.WebApi.AuthGetAction("Task", "Reexecute", "1.0")]
    public class ReexecuteAction : BaseAction<ReexecuteParams, ReexecuteResult>
    {
        protected override ActionResult<ReexecuteResult> OnExecute(ActionContext context, SysParameters sysParams, ReexecuteParams opParams)
        {
            SER.TaskService service = new SER.TaskService();
            string[] batchArr = opParams.Batchs.Split(new char[]{','});
            List<Guid> batchIds = new List<Guid>();
            for (int index = 0; index < batchArr.Length; index++)
            {
                batchIds.Add(new Guid(batchArr[index]));
            }
            ServiceResult<VIW.VReexecuteResult>  serResult = service.Reexecute(context.UserId, opParams.TaskId, batchIds);

            if(serResult.IsSuccess)
            {
                ReexecuteResult actResult = new ReexecuteResult();
                for (int index = 0; index < serResult.Data.Items.Count; index++)
                {
                    ReexecuteResult.Item item = new ReexecuteResult.Item();
                    item.BatchId = serResult.Data.Items[index].BatchId;
                    item.ErrorCode = serResult.Data.Items[index].ErrorCode;
                    item.Message = serResult.Data.Items[index].Message;
                    item.IsSuccess = serResult.Data.Items[index].IsSuccess;
                    item.OldBatchId = serResult.Data.Items[index].OldBatchId;
                    item.OldCreatedTime = serResult.Data.Items[index].OldCreatedTime;

                    VIW.VReexecuteResult.Item vrrItem = serResult.Data.Items[index];
                    for (int pIndex = 0; pIndex < vrrItem.Parameters.Count; pIndex++)
                    {
                        ReexecuteResult.Item.Parameter parameter = new ReexecuteResult.Item.Parameter();
                        parameter.Name = vrrItem.SourceParameters[pIndex].Name;
                        parameter.Type = vrrItem.SourceParameters[pIndex].DataType;
                        parameter.Value = vrrItem.Parameters[pIndex].ValueString;
                        item.Parameters.Add(parameter);
                    }

                    actResult.Items.Add(item);
                }
                return new ActionResult<ReexecuteResult>(actResult);
            }
            else
            {
                return new ActionResult<ReexecuteResult>(serResult.IsSuccess, serResult.ErrorCode, serResult.Message, null);
            }
        }
    }

    public class ReexecuteParams
    {
        /// <summary>
        /// 任务id
        /// </summary>
        public Guid TaskId { get; set; }

        /// <summary>
        /// 批次信息(batchid,batchid,batchid)
        /// </summary>
        public string Batchs { get; set; }
    }

    public class ReexecuteResult
    {
        public List<Item> Items = new List<Item>();

        public class Item
        {
            public Guid OldBatchId { get; set; }
            public DateTime OldCreatedTime { get; set; }

            public Guid BatchId { get; set; }

            public bool IsSuccess { get; set; }

            public int ErrorCode { get; set; }

            public string Message { get; set; }

            public List<Parameter> Parameters = new List<Parameter>();

            public class Parameter
            {
                public string Name { get; set; }

                public string Type { get; set; }

                public string Value { get; set; }
            }
        }
    }
}
